/**
 * 
 */
package com.android.hw.projectmenu;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.qualcomm.qcnvitems.QcNvItems;
/**
 * @author gaoshengpan
 *
 */
public class MMITestResultDetails extends ListActivity {
	private String[] mTestResultString;
	private QcNvItems mNv;
	private int position;
	private final static String tab = "    ";

	Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
        	 Log.d("MMIDetails", "handleMessage--2-->"+position);
        	/* 15-09-02 gaoshenpan fix 2499 isnull crash begin*/
            if(mNv != null){
        	setResultInfo();
        	 }
            /* 15-09-02 gaoshenpan fix 2499 isnull crash end*/
        	

        }
    };

	/**
	 * 
	 */
	public MMITestResultDetails() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mmi_result);
		position =getIntent().getIntExtra("position", 0);
		mNv = new QcNvItems(this);
		 Log.d("MMIDetails", "onCreate--1-->"+position);
		if(position == 0){
			String[] s1 = getResources().getStringArray(
					R.array.mmi1_test);
			 mTestResultString = s1;
			 Log.d("MMIDetails", "onCreate--2-->"+position);
		}else{
			String[] s1 = getResources().getStringArray(
					R.array.mmi1_test);
			 mTestResultString = s1;
			 Log.d("MMIDetails", "onCreate--2-->"+position);
			 Log.d("MMIDetails", "onCreate--3-->"+position);
		}

	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (mNv == null) {
            mNv = new QcNvItems(this);
        }
		mHandler.sendEmptyMessageDelayed(0, 2000);
		
	}
	
	private void setResultInfo() {
//		Log.d("MMIDetails", "setResultInfo--2-->"+getResultDetails());
		
		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getResultDetails()));
	}
	
	private String[] getResultDetails() {
		String NV2499 = "";
		String mmi1String = "";
		String mmi2String = "";
		try {
			NV2499 = mNv.getNvFactoryData3I();
		} catch (Exception e) {
			e.printStackTrace();
		}
		/* 15-09-02 gaoshenpan fix 2499 isnull crash begin*/
		if(!NV2499.equals("")){
		/* 15-09-21 gaoshenpan ad versiontest begin*/
		mmi1String = NV2499.substring(28, 57).replace("B", "");
		mmi2String = NV2499.substring(57, 86).replace("B", "");
		/* 15-09-21 gaoshenpan ad versiontest end*/
		mHandler.removeMessages(0);
		}else{
			mHandler.sendEmptyMessageDelayed(0, 2000);
			Toast.makeText(this, getString(R.string.nv_not_ready),
					Toast.LENGTH_SHORT).show();
		}
		Log.d("CJ", "mTestResultString.length"+mTestResultString.length);
		Log.d("CJ", "mmi1String.length()"+mmi1String.length());
		Log.d("CJ", "mmi2String.length()"+mmi2String.length());
		
		if (position == 0) {
			for (int i = 0; i < mTestResultString.length; i++) {
				if(!NV2499.equals("")){
				if (mmi1String.substring(i, i+1).equals("P")) {
					mTestResultString[i] = mTestResultString[i] + tab + getString(R.string.success);
				} else if (mmi1String.substring(i, i+1).equals("F")) {
					mTestResultString[i] = mTestResultString[i] + tab + getString(R.string.fail);
				} else if (mmi1String.substring(i, i+1).equals("U")) {
					mTestResultString[i] = mTestResultString[i] + tab + getString(R.string.result_not_tested);
				}
				}else{
					mTestResultString[i] = mTestResultString[i] ;
				}
				Log.d("MMIDetails","getResultDetails--i="+i+"mTestResultString[i] =" +mTestResultString[i] );
			}
			
			
		} else {
			
			for (int i = 0; i < mTestResultString.length; i++) {
				if(!NV2499.equals("")){
				if (mmi2String.substring(i, i+1).equals("P")) {
					mTestResultString[i] = mTestResultString[i] + tab + getString(R.string.success);
				} else if (mmi2String.substring(i, i+1).equals("F")) {
					mTestResultString[i] = mTestResultString[i] + tab + getString(R.string.fail);
				} else if (mmi2String.substring(i, i+1).equals("U")) {
					mTestResultString[i] = mTestResultString[i] + tab + getString(R.string.result_not_tested);
				}
				}else{
					mTestResultString[i] = mTestResultString[i];
				}
			}
			/* 15-09-02 gaoshenpan fix 2499 isnull crash end*/
		}
		Log.d("MMIDetails", "getResultDetails--2-->"+mTestResultString.toString());
		return mTestResultString;
	}
	
	/* (non-Javadoc)
	 * @see android.app.ListActivity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mNv != null) {
            mNv.dispose();
        }
		/* 15-09-02 gaoshenpan fix 2499 isnull crash begin*/
		mHandler.removeMessages(0);
		/* 15-09-02 gaoshenpan fix 2499 isnull crash end*/
	}
	
}
